<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="场景主题名称" prop="sensorsubject">
        <el-input
          v-model="queryParams.sensorsubject"
          placeholder="请输入传感器主题名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主题说明" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入主题说明"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="submitForm">确定</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="sensorsubjectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" v-if="false" />
      <el-table-column label="传感器主题名称" align="center" prop="sensorsubject" />
      <el-table-column label="主题说明" align="center" prop="description" /> 
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> 
  </div>
</template>

<script>
import { listSensorsubject, getSensorsubject, delSensorsubject, addSensorsubject, updateSensorsubject } from "@/api/snmp/sensorsubject";

import {listSubjectdevice,  addlistSensubjectdevice } from "@/api/snmp/sensubjectdevice";

export default {
  name: "Sensorsubject",
  props: ['deviceID','deviceName','deviceIp'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 传感器主题信息表格数据
      sensorsubjectList: [],
      sensubjectdeviceList:[],
      //场景主题信息
      subjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sensorsubject: null,
        description: null
      },
      //保存数据
      queryPut:[],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sensorsubject: [
          { required: true, message: "传感器主题名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
   /** 查询传感器主题和设备关联数据 */
    getcheckList() {
      this.loading = true;
      listSubjectdevice(this.deviceID).then(response => {
        this.sensubjectdeviceList = response.data;
      });
    },
    /** 查询传感器主题信息列表 */
    getList() {
      this.loading = true;
      listSensorsubject(this.queryParams).then(response => {
        this.sensorsubjectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() { 
    },
    // 表单重置
    reset() {
      this.form = { 
        id: null,
        sensorsubject: null,
        deviceId: null,
        deviceName: null,
        deviceIp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.subjectList=selection.map(item => item.sensorsubject);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 提交按钮 */
    submitForm() { 
      this.reset();
      this.queryPut=[];
      for (let i in this.subjectList) {
        if (this.subjectList[i]) 
        { 
          this.form.deviceId=this.deviceID;
          this.form.deviceIp=this.deviceIp;
          this.form.deviceName=this.deviceName;
          this.form.sensorsubject=this.subjectList[i];
          this.queryPut.push(this.form);
        }
      }
      addlistSensubjectdevice(this.queryPut).then(response => {
        this.$modal.msgSuccess("设备关联传感器信息成功");
       
      }) .catch(error => {
        this.$modal.msgError("提交失败");
        });

    }
  }
};
</script>
